(ns user.commonFunctions
	(:import (javax.swing JFrame JPanel JButton JLabel table.DefaultTableModel JTable JScrollPane JTextField)
        (java.awt.event ActionListener)
	(java.awt BorderLayout Dimension Checkbox Container GridBagLayout GridBagConstraints Font))
)

;;------------------------------------------------------------------------------------------------------------------------------------

(defn check-num-fields-find 
	"Cheks if the string-values passed are all integers and returns a boolean indicating so.
	vect -- the vector containing the string-values to be checked"
	[vect]
	(let [result (atom true)]
		(doseq [[elem] vect] (try ( if (not(empty?(.trim (str elem)))) (. Integer decode (str elem))) (catch Exception e 
			(if (= NumberFormatException (.getClass e)) (reset! result false)))))
@result))

;;------------------------------------------------------------------------------------------------------------------------------------

(defn check-num-fields 
	"Cheks if the string-values passed are all floats and returns a boolean indicating so.
	vect -- the vector containing the string-values to be checked"
	[vect]
	(let [result (atom true)]
			(doseq [[elem] vect] (try (. Float parseFloat (str elem)) (catch Exception e 
			(if (= NumberFormatException (.getClass e)) (reset! result false)))))
@result))

;;------------------------------------------------------------------------------------------------------------------------------------

(defn format-rate 
	 "Formats the string rate into a currency format
	 rate -- a string containing a number which will be returned with $ and two decimals"
	 [rate]
	 (if (not (empty? rate))
	 	 (if (< (.indexOf rate ".") 0) 
			(apply str ["$" rate ".00"])
			(if (= (count (.substring rate (.indexOf rate "."))) 2)
				       (apply str ["$" rate "0"])
			(apply str ["$" rate]))
		 )
	 )
)

;;------------------------------------------------------------------------------------------------------------------------------------

(defn check-date
	"Checks the components of a date (month day year) passed as arguments to see if none of them gets out of bounds
	dd -- string number that represents the day
	mm -- string number that represents the month
	yyyy -- string number that represents the year"
	[dd mm yyyy]
	(let [result (atom true)]
		(if (or (not (check-num-fields [dd mm yyyy])) (< (count yyyy) 4))
			(reset! result false)
			(let [day (. Integer parseInt (str dd))
				month (. Integer parseInt (str mm))]
						(if (or (<= month 0) (> month 12)) 
							(reset! result false)
							(if (or (or (<= day 0) (> day 31)) (and (= month 2) (> day 29)))
								(reset! result false)
@result))))))
			
;;------------------------------------------------------------------------------------------------------------------------------------

(defn format-date
	"From the given day month and year, returns a string containing the date in a nice YYYY/MM/DD format
	dd -- string number representing the day
	mm -- string number representing the month
	yyyy -- string number representing the year"
	[dd mm yyyy]
	(if (< (count dd) 2)
		(if (< (count mm) 2)
			(apply str [yyyy "/" "0" mm "/" "0" dd])
			(apply str [yyyy "/" mm "/" "0" dd]))
		(if (< (count mm) 2)
			(apply str [yyyy "/" "0" mm "/" dd])
(apply str [yyyy "/" mm "/" dd]))))

;;------------------------------------------------------------------------------------------------------------------------------------

(defn addRow-table
 	"Add a record into the table.
 	number -- the record number that will be added
 	dictionary -- a map with all the data to be added
 	model -- the table model of the JTable where de rows will be added"
    [number dictionary model]
       (let [bool		(:deleted dictionary)
       	     nme   		(:name dictionary)
       	     loc		(:location dictionary)
       	     rate		(:rate dictionary)
       	     owner		(:owner dictionary)
       	     date		(:date dictionary)
       	     size		(:size dictionary)
       	     ;smoking	(:smoking dictionary)
       	     smoking	(if (= "Y" (:smoking dictionary)) true false) 
       	     data 		{:name nme :location loc :size size
       	     			 :smoking smoking :rate rate :date date 
       	     			 :owner owner :recno number}]
       	     
       	       (if (.equals bool "false")(.addRow model (to-array(vals data))))))

;;------------------------------------------------------------------------------------------------------------------------------------
       	       
(defn upperLetter
		"Capitalizes the first character of the given word and returns it"
		[word]
		(if (> (count word) 1) (apply str (concat (str (. Character toUpperCase 
						(.charAt word 0))) (.toLowerCase (.substring word 1))))
						(if (not (empty? (.trim word))) 
						(str (. Character toUpperCase (.charAt word 0))) word)))
		
